package cn.wuqingna.legendgem.api.attribute.imp

import cn.wuqingna.legendgem.api.attribute.AbsAttributeSource
import cn.wuqingna.legendgem.api.attribute.AttributeData
import cn.wuqingna.legendgem.gem.GemManager
import cn.wuqingna.legendgem.loader.GemLoader
import cn.wuqingna.legendgem.util.isDouble
import org.bukkit.Bukkit
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import taboolib.module.nms.getItemTag
import taboolib.platform.util.isAir
import taboolib.platform.util.isNotAir
import java.util.UUID

class GemSource(
    val uuid: UUID,
    val item: MutableList<ItemStack>
): AbsAttributeSource() {
    override fun getAttribute(): AttributeData? {
        val player = Bukkit.getPlayer(uuid) ?: return null
        val attr = mutableListOf<String>()
        val customDate = mutableMapOf<String, Double>()
        item.forEach {
            if (it.isNotAir()) {

                attr += GemManager.getItemAttribute(it, player)
                if (it.getItemTag().getDeep("legendGem.gem-key") == null) { return@forEach }
                val key = it.getItemTag().getDeep("legendGem.gem-key")!!.asString()
                val gem = GemLoader.gems[key]!!
                gem.customData.forEach { (key, value) ->
                    if (customDate.containsKey(key)) {
                        if (value.isDouble()){
                            customDate[key] = customDate[key]!! + value.toDouble()
                        } else {
                            customDate[key] = customDate[key]!! + 1
                        }
                    } else {
                        if (value.isDouble()){
                            customDate[key] = value.toDouble()
                        } else {
                            customDate[key] = 1.0
                        }

                    }
                }
            }
        }
        return AttributeData(attr, customDate)
    }
}